/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ViewSched.java
 *
 * Created on Jul 24, 2012, 4:42:09 PM
 */
package monitor;

import java.awt.Color;
import java.awt.Component;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;
import java.awt.Container;
import java.sql.SQLException;

/**
 *
 * @author owner
 */
public class ViewSched extends javax.swing.JFrame {

    /** Creates new form ViewSched */
    
    String SchedID;
    String mode = "Sched";
    String num;
    Long temp;
    private Component frame;
    private Container c;
    public ViewSched() {
        initComponents();
        
        c = getContentPane();
        c.setBackground(Color.white);
        schedInfo.setGridColor(Color.green);
        
        refresh();        
    }
    
    private void refresh(){
        QHandler queryHandler = new QHandler();
        DBManager dbconnector = new DBManager();
        dbconnector.Connect();
        queryHandler.viewSched();
        dbconnector.Pull(queryHandler.query);
        schedInfo.setModel(dbconnector.dTableModel);
        dbconnector.Disconnect(dbconnector.rs);
        
        TableColumn col = schedInfo.getColumnModel().getColumn(0);
        col.setPreferredWidth(1);
        JTableHeader header = schedInfo.getTableHeader();
        header.setBackground(Color.green);
        
        
        schedInfo.getColumnModel().getColumn(0).setHeaderValue("Sched ID");
        schedInfo.getColumnModel().getColumn(1).setHeaderValue("Tutor Name");
        schedInfo.getColumnModel().getColumn(2).setHeaderValue("Tutee Name");
        schedInfo.getColumnModel().getColumn(3).setHeaderValue("Day");
        schedInfo.getColumnModel().getColumn(4).setHeaderValue("Time");
        schedInfo.getColumnModel().getColumn(5).setHeaderValue("Course");
  
        
        editList.removeAllItems();
        editList.addItem("");
     
        
         QHandler queryHandler1 = new QHandler();
        DBManager dbconnector1 = new DBManager();
        dbconnector1.Connect();
        queryHandler1.selectIdSched();
        dbconnector1.PullID(queryHandler1.query, mode);
        dbconnector1.Disconnect(dbconnector1.rs);
        
        
        QHandler queryHandler2 = new QHandler();
        DBManager dbconnector2 = new DBManager();
        dbconnector2.Connect();
        queryHandler2.viewTotalAssignments();
        dbconnector2.getInfo(queryHandler2.query);
        
        try{
            
          temp = dbconnector2.rs.getLong("total");
          num = temp.toString();
             
        schedTotal.setText(num);
        }catch(SQLException ex){}
        
         dbconnector2.Disconnect(dbconnector2.rs);
        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        schedInfo = new javax.swing.JTable();
        BtnClose = new javax.swing.JButton();
        BtnUpdate = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        editList = new javax.swing.JComboBox();
        BtnDelete = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        schedTotal = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("View Schedules");

        jLabel1.setText("VIEW TUTORING SCHEDULE");

        schedInfo.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null}
            },
            new String [] {
                "#", "Tutor Name", "Tutee Name", "Day", "Time", "Tutoring Course"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, true, false, false, true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        schedInfo.setSelectionBackground(new java.awt.Color(255, 255, 255));
        jScrollPane1.setViewportView(schedInfo);
        schedInfo.getColumnModel().getColumn(0).setResizable(false);
        schedInfo.getColumnModel().getColumn(0).setPreferredWidth(2);
        schedInfo.getColumnModel().getColumn(1).setResizable(false);
        schedInfo.getColumnModel().getColumn(5).setPreferredWidth(30);

        BtnClose.setFont(new java.awt.Font("Modern No. 20", 0, 18));
        BtnClose.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pictures/Cancel.png"))); // NOI18N
        BtnClose.setText("Close");
        BtnClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnCloseActionPerformed(evt);
            }
        });

        BtnUpdate.setFont(new java.awt.Font("Modern No. 20", 0, 18));
        BtnUpdate.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pictures/update.png"))); // NOI18N
        BtnUpdate.setText("Update");
        BtnUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnUpdateActionPerformed(evt);
            }
        });

        jLabel2.setText("SELECT SCHEDULE");

        BtnDelete.setFont(new java.awt.Font("Modern No. 20", 0, 18));
        BtnDelete.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pictures/delete.png"))); // NOI18N
        BtnDelete.setText("Delete");
        BtnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnDeleteActionPerformed(evt);
            }
        });

        jLabel3.setText("TOTAL # OF ASSIGNMENTS");

        schedTotal.setText("NUM");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 888, Short.MAX_VALUE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 277, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(editList, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 415, Short.MAX_VALUE)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(schedTotal)
                        .addGap(23, 23, 23))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(BtnUpdate)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(BtnDelete)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(BtnClose)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(4, 4, 4)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(7, 7, 7)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(schedTotal)
                    .addComponent(jLabel2)
                    .addComponent(editList, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(BtnClose)
                    .addComponent(BtnDelete)
                    .addComponent(BtnUpdate))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void BtnCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnCloseActionPerformed
// TODO add your handling code here:
    ViewOptions view = new ViewOptions();
    view.setVisible(true);
    view.setLocationRelativeTo(null);
    this.dispose();
}//GEN-LAST:event_BtnCloseActionPerformed

private void BtnUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnUpdateActionPerformed
// TODO add your handling code here:
    if(editList.getSelectedItem().toString().equals("")){
        JOptionPane.showMessageDialog(frame,
         "Database might be empty or you selected an invalid value",
        "Cannot process request",
        JOptionPane.WARNING_MESSAGE);
    }
    
    else{
    UpdateSched updateSched = new UpdateSched();
    updateSched.SchedID = editList.getSelectedItem().toString();
    updateSched.Update();
    updateSched.setVisible(true);
    updateSched.setLocationRelativeTo(null);
    this.dispose();
    }
}//GEN-LAST:event_BtnUpdateActionPerformed

private void BtnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnDeleteActionPerformed
// TODO add your handling code here
     if(editList.getSelectedItem().toString().equals("")){
        JOptionPane.showMessageDialog(frame,
         "Database might be empty or you selected an invalid value",
        "Cannot process request",
        JOptionPane.WARNING_MESSAGE);
    }
    
    else{
    Delete deleteSched = new Delete();
    deleteSched.SchedID = editList.getSelectedItem().toString();
    deleteSched.DeleteSched();
    refresh();
     }
}//GEN-LAST:event_BtnDeleteActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ViewSched.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ViewSched.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ViewSched.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ViewSched.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new ViewSched().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BtnClose;
    private javax.swing.JButton BtnDelete;
    private javax.swing.JButton BtnUpdate;
    public static javax.swing.JComboBox editList;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable schedInfo;
    private javax.swing.JLabel schedTotal;
    // End of variables declaration//GEN-END:variables
}
